package com.example.myapplication.handlerDemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.example.myapplication.R;

public class HandlerDemoActivity extends AppCompatActivity {
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case TEST:
                    Log.e("currentThread2", "run: "+Thread.currentThread().getId() );
                    txt.setText("工作线程：这是一段文字");
                    break;
            }
            super.handleMessage(msg);
        }
    };
    TextView txt  = null;
    final static int TEST = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_demo);
        Log.e("currentThread0", "run: "+Thread.currentThread().getId() );
        txt = findViewById(R.id.hello);
        new ActivityThread().start();
    }
    class ActivityThread extends Thread{
        public void run(){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.e("currentThread1", "run: "+Thread.currentThread().getId() );


            Message message = new Message();
            message.what = TEST;
            handler.sendMessage(message);
        }
    }
}